﻿using System.Windows;
using System.Windows.Controls;
using Chambills.Present.Core;
using NUnit.Framework;

namespace Present.Core.Tests
{
    [TestFixture, RequiresSTA]
    public class ViewRegistryTestCase
    {
        private MockGrid view;
        private ViewRegistry registry;

        [SetUp]
        public void SetUp()
        {
            TestViewRegistry.Clear();
            view = new MockGrid() { DataContext = this };
            registry = new ViewRegistry();
        }


        [Test]
        public void ShouldUnRegisterViewWhenViewUnloaded()
        {
            view.SetValue(ViewRegistry.RegisterProperty, true);
            view.RaiseLoaded();
            view.RaiseUnloaded();
            Assert.IsNull(registry.FindViewFor(this));
        }

        [Test]
        public void ShouldBeAbleToRetreiveRegisteredView()
        {
            view.SetValue(ViewRegistry.RegisterProperty, true);
            view.RaiseLoaded();
            Assert.AreSame(view, registry.FindViewFor(this));
        }

        class TestViewRegistry : ViewRegistry
        {
            public static void Clear()
            {
                Views.Clear();
            }
        }


    }


    public class MockGrid : Grid
    {
        public void RaiseLoaded()
        {
            RaiseEvent(new RoutedEventArgs(LoadedEvent));
        }

        public void RaiseUnloaded()
        {
            RaiseEvent(new RoutedEventArgs(UnloadedEvent));
        }
    }
}
